import js from '@eslint/js'
import globals from 'globals'
import tseslint from 'typescript-eslint'
import pluginVue from 'eslint-plugin-vue'
import { defineConfig } from 'eslint/config'
import fs from 'fs'
import path from 'path'

// 读取 auto-import 全局变量配置
const autoImportJsonPath = path.resolve(
  __dirname,
  './.eslintrc-auto-import.json'
)
const autoImportConfig = JSON.parse(
  fs.readFileSync(autoImportJsonPath, 'utf-8')
)

// 合并浏览器全局变量和 auto-import 全局变量
const mergedGlobals = {
  ...globals.browser,
  ...autoImportConfig.globals,
}

export default defineConfig([
  {
    files: ['**/*.{js,mjs,cjs,ts,mts,cts,vue}'],
    plugins: { js },
    extends: ['js/recommended'],
    languageOptions: { globals: mergedGlobals },
  },
  tseslint.configs.recommended,
  pluginVue.configs['flat/essential'],
  {
    files: ['**/*.vue'],
    languageOptions: { parserOptions: { parser: tseslint.parser } },
  },
])
